const express = require('express')
const router = express.Router()
// 导入已封装的数据函数
const db = require('../../sql/db.js')

// 访问地址：
// http://localhost:3005/vote/add?id=xxxx11
router.get('/add',function(req,res){
    // 前端接收参数
    let id = req.query.id;
    // console.log({id});
    let isVoted = req.query.isVoted - 0 ;
    // 判断是否已投票
    if(isVoted >= 1){
        res.send({
            code: 200,
            msg: "该用户已经投过票"
        })
    }else {
        // 查询语句
        // let sql = 'select * from User where id = "'+id+'"';
        let sql = 'select * from User where id = "'+id+'"';
        // 调用查询方法
        db.query(sql,function(err, rows){
            if(err){
                res.send({code: -1, msg: '查询失败'})
            }else {
                // console.log('rows:',rows);
                if(rows.length == 0){
                    res.send({code: 404, msg: '查无此人或缺少值'});
                }
                else 
                // 判断用户票数是否为0以及数据表是有数据
                if(rows[0].count == 0 && id!= rows[0].id){
                    let [{userName,count}] = rows;
                    count = 1;
                    // 当前记录的ID
                    let vid = Math.random().toString().slice(2);
                    // 插入数据
                    let sql = 'insert into vote(userName,id,vid,count) values("'+userName+'","'+id+'","'+vid+'","'+count+'")';
                    db.query(sql, (err) => {
                        if(err){
                            res.send({code: -1, msg: '插入失败',err})
                        }else {
                            // 更新数据
                            let sql = 'update User set count = "'+count+'" where id="'+id+'"';
                            db.query(sql, function (err) {
                                if (err) {
                                    res.send({ code: -1, message: "修改失败"});
                                } 
                            });
                            res.send({
                                code: 200,
                                msg: '投票成功',
                                isVoted: 2,
                                count: count+"票",
                                userName
                            });
                        }
                    })
                }else {
                    let [{userName,count}] = rows;
                    count +=1;
                    // 更新数据
                    let sql = 'update vote set count = "'+count+'" where id="'+id+'"';
                    db.query(sql, function (err) {
                        if (err) {
                            res.send({ code: -1, message: "修改失败"});
                        } else {
                            // 更新数据
                            let sql = 'update User set count = "'+count+'" where id="'+id+'"';
                            db.query(sql, function (err) {
                                if (err) {
                                    res.send({ code: -1, message: "修改失败"});
                                } 
                            });
                            res.send({ code: 200, message: "修改成功",isVoted: 2,count: count+"票",userName});
                        }
                    });
                }
            }
        });
    }
})

module.exports = router;